﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using COFE.Core.Utils;

namespace COFE.Core.Actions
{
    public static class IEntryListActionExtension
    {
        public static object[] GetSupportedAction(this IEntryList entryList)
        {
            return entryList.GetSupportedProperty(PropertyRetrivalOptions.NotApplied,MatchMode.Any, new ActionPropertyFilter());
        }

        public static string[] GetSupportedActionString(this IEntryList entryList)
        {
            return (from a in entryList.GetSupportedAction()
                    select EnumUtils.GetResourceName(a)).ToArray();
        }

        public static bool HasAction(this IEntryList entryList, object action)
        {
            return entryList.GetSupportedAction().Contains(action);
        }

        public static bool HasAction(this IEntryList entryList, string actionStr)
        {
            return entryList.GetSupportedActionString().Contains(actionStr);
        }

        public static Func<IEntryList, string, IWorkProgress, IEntryList> GetAction(this IEntryList entryList, object action)
        {
            if (action == null)
                throw new NullReferenceException();
            var pp = entryList.GetProperty(action, PropertyRetrivalOptions.NotApplied);
            if (pp == null)
                throw new NotSupportedException();

            return (Func<IEntryList, string, IWorkProgress, IEntryList>)entryList.GetProperty(action).Value;
        }

        public static Func<IEntryList, string, IWorkProgress, IEntryList> GetAction(this IEntryList entryList, string actionStr)
        {
            var action = COFEShared.PropertyDictionary.GetProperty(actionStr);
            if (action == null)
                throw new NotSupportedException(actionStr);
            return entryList.GetAction(action);
        }

        public static IEntryList InvokeAction(this IEntryList entryList, object action, string param, IWorkProgress workProgress)
        {
            var actionFunc = entryList.GetAction(action);            
            return actionFunc(entryList, param, workProgress);
        }

        public static IEntryList InvokeAction(this IEntryList entryList, string actionStr, string param, IWorkProgress workProgress)
        {
            var actionFunc = entryList.GetAction(actionStr);
            return actionFunc(entryList, param, workProgress);
        }
    }
}
